package com.treasure.service.collocate.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.treasure.service.BaseService;
import com.treasure.service.collocate.service.BannerService;
import com.treasure.service.commons.lang.util.BeanUtil;
import com.treasure.service.commons.lang.util.StringUtil;
import com.treasure.service.domain.TBanner;
import com.treasure.service.domain.TBannerExample;
import com.treasure.service.dto.TBannerDto;
import com.treasure.service.mapper.TBannerMapper;
import com.treasure.service.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * Banner配置信息
 *
 * @author <a href="mailto:panxinwen@yingu.com">Pan Xinwen</a>
 * @version 1.0 2018-10-30 11:06
 */
@Service
public class BannerServiceImpl extends BaseService implements BannerService {

    @Autowired
    private TBannerMapper bannerMapper;

    @Override
    public PageResult<List<TBannerDto>> getBannerList(Integer pageSize, Integer pageNum, Integer status) {

        Page page = PageHelper.startPage(pageNum,pageSize);

        TBannerExample example = new TBannerExample();
        TBannerExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(status)){
            criteria.andStatusEqualTo(status);
        }

        example.setOrderByClause("create_time desc");

        List<TBanner> list = bannerMapper.selectByExample(example);

        List<TBannerDto> result = new ArrayList<>();

        if (list != null){

            list.forEach(tBanner -> {

                result.add(BeanUtil.copy(tBanner,TBannerDto::new));

            });

        }

        return new PageResult(page,result);
    }

    @Override
    public Integer updateBanner(TBanner tBanner) {
        if (StringUtil.isEmpty(tBanner.getId())){

            return bannerMapper.insert(tBanner);

        }else {
            return bannerMapper.updateByPrimaryKeySelective(tBanner);
        }
    }
}
